package light.mvc.controller.hrs;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.pageModel.base.Grid;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.hrs.HRSData;
import light.mvc.service.hrs.HRSDataServiceI;

import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/hrsdata")
public class HRSDataController extends BaseController{

	@Autowired
	private HRSDataServiceI dataService;
	
	@RequestMapping("/manager")
	public String manager() {
		return "/admin/habitus_research_sys/data";
	}
 
	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(HRSData data, PageFilter ph) {
//		spring自动将参数注入到ph对象中
		Grid grid = new Grid();
		grid.setRows(dataService.dataGrid(data, ph));
		grid.setTotal(dataService.count(data, ph));
		return grid;
	}

	@RequestMapping("/delete")
	@ResponseBody
//	ids为jsonArray
	public Json delete(String ids,HttpServletRequest request) {
		JSONArray jsonIds;
		Json j = new Json();
		try {
			jsonIds = new JSONArray(ids);
			for (int i = 0; i < jsonIds.length(); i++) {
				long id = jsonIds.getLong(i);
//				获取dataId
				HRSData r = dataService.get(id);
//				删除资源
				dataService.delete(id,request);
			}
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (JSONException e1) {
			e1.printStackTrace();
			j.setMsg(e1.getMessage());
		}catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/detailPage")
	public String detailPage(HttpServletRequest request, Long id) {
		HRSData r = dataService.get(id);
		request.setAttribute("data", r); 
		return "/admin/habitus_research_sys/dataDetail";
	}
	

}
